﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class D05_Quaternion : MonoBehaviour
{
    public Transform other;
    private void OnGUI()
    {
        if (GUILayout.Button(" 四元数转欧拉角 "))
        {
            Quaternion rotation = transform.rotation;
            Vector3 euler = transform.rotation.eulerAngles;
            print(euler);
        }
        if (GUILayout.Button(" 欧拉角转四元数 "))
        {
            Quaternion rotation = Quaternion.Euler(0, 60, 0); ;
            transform.rotation = rotation;
        }

        if (GUILayout.Button(" 四元数也可以求夹角 "))
        {
            print(Quaternion.Angle(transform.rotation, other.rotation));
            //其实这个角度就是两个物体前方的夹角:
            print(Vector3.Angle(transform.forward, other.forward));
        }
    }

    //插值和other的旋转同步
    private void Update1()
    {
        transform.rotation = Quaternion.Lerp(transform.rotation, other.rotation, Time.deltaTime * 2);
    }
    //演示lookRotation
    private void Update()
    {
        //得到一个朝向 other的向量
        Vector3 dir = other.position - transform.position;
        //根据这个向量得到一个旋转
        Quaternion rotation = Quaternion.LookRotation(dir);

        // transform.rotation = rotation; //这句话等同LookAt

        transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * 2);
    }
}